package require wt::database::drivers::tclOdbc::sqlServer

namespace eval ::wt::server::dataSources::wtSqlServer:: {
  variable base ::wt::database::drivers::tclOdbc::sqlServer
  variable connectionString "Driver=SQL Server;Server=.\\SQLEXPRESS"
  variable dbName wt5
}

# Open database connection

proc ::wt::server::dataSources::wtSqlServer::openDataSource {dataSource} {
  upvar $dataSource ds
  variable connectionString
  variable dbName
  return [database connect [table get $ds id] "$connectionString;Database=$dbName"]
}

# Setup the database schema

proc ::wt::server::dataSources::wtSqlServer::setupDatabase {dataSource {createDb 1} {schemaFilePath {}}} {
  upvar $dataSource ds

  variable dbName
  puts "Setting up database $dbName."

  variable connectionString
  set conn [database connect [table get $ds id] $connectionString]
  ::wt::database::dbManager::setDataSourceConnection [table get $ds id] $conn
  set ds [::wt::database::dbManager::getDataSource [table get $ds id]]

  set sql "if not exists(select * from master..sysdatabases where name = '$dbName') create database $dbName"
  $conn $sql

  $conn "use $dbName"

  if {$schemaFilePath == ""} {
    set schemaFilePath {\Program Files\Tcl\lib\wt\server\setup\schemas\sqlServer\wtSqlServer.sql}
  }
  ::wt::database::drivers::tclOdbc::sqlServer::executeFile ds $schemaFilePath

  $conn "use $dbName"

  puts "Finished setting up database $dbName."
}

package provide wt::server::dataSources::wtSqlServer 1
